package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.math.NumberUtils;

import models.Point;
import models.PointWall;
import models.Updates;
import models.User;
import play.db.jpa.Model;
import play.mvc.Controller;
import play.mvc.WebSocketController;
import play.mvc.With;
import services.UpdatesService;
import services.UserService;

@With(Secure.class)
public class Application extends Controller {
    @Inject
    private static UserService userService;
    @Inject
    private static UpdatesService updatesService;
    
    public static void index() {
        List<User> users = userService.selectAll();
        render(users);
    }
    
    public static void profile() {
        render();
    }
    
    public static void notice() {
    	String notices = params.get("notices");
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Updates update = new Updates();
    	update.setUsername(Secure.Security.connected());
    	update.setNotices(notices);
    	update.setStr_date(sdf.format(date));
    	User user = userService.getUserByUsername(Secure.Security.connected());
    	update.setUser_id(user.getId().intValue());
    	List<Updates> updates = updatesService.getNotifications(update);
    	renderJSON(updates);
	}
    
    public static void globalnotice() {
    	String notices = params.get("notices");
    	Updates update = new Updates();
    	update.setUsername(Secure.Security.connected());
    	update.setNotices(notices);
    	User user = userService.getUserByUsername(Secure.Security.connected());
    	update.setUser_id(user.getId().intValue());
    	List<Updates> updates = updatesService.getNotifications(update);
    	renderJSON(updates);
	}
    public static void shareJourney() {
    	String user_sh_id = params.get("user_id");
    	String jour_id = params.get("journey_id");
    	Updates update = new Updates();
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	update.setUsername(Secure.Security.connected());
    	//update.setNotices(notices);
    	int journey_id  = (NumberUtils.isNumber(jour_id)) ? Integer.valueOf(jour_id) : 0;
    	int user_shared_id  = (NumberUtils.isNumber(user_sh_id)) ? Integer.valueOf(user_sh_id) : 0;
    	update.setJourney_id(journey_id);
    	update.setUser_shared_id(user_shared_id);
    	User user = userService.getUserByUsername(Secure.Security.connected());
    	update.setUser_id(user.getId().intValue());
    	update.setStr_date(sdf.format(date));
    	//update.getUser_id();
    	updatesService.shareJourney(update);
    	//render();
	}
    public static void sharePosition() {
    	String user_sh_id = params.get("user_id");
    	String pos_id = params.get("position_id");
    	Updates update = new Updates();
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	update.setUsername(Secure.Security.connected());
    	//update.setNotices(notices);
    	int position_id  = (NumberUtils.isNumber(pos_id)) ? Integer.valueOf(pos_id) : 0;
    	int user_shared_id  = (NumberUtils.isNumber(user_sh_id)) ? Integer.valueOf(user_sh_id) : 0;
    	update.setPosition_id(position_id);
    	update.setUser_shared_id(user_shared_id);
    	User user = userService.getUserByUsername(Secure.Security.connected());
    	update.setUser_id(user.getId().intValue());
    	update.setStr_date(sdf.format(date));
    	//update.getUser_id();
    	updatesService.sharePosition(update);
    	//render();
	}
    public static void friendRequest(){
    	String sha_id = params.get("share_id");
    	String user_id = params.get("user_id");
    	String flag =  params.get("flag_cancel");

    	int share_id  = (NumberUtils.isNumber(sha_id)) ? Integer.valueOf(sha_id) : 0;
    	int friend_id  = (NumberUtils.isNumber(user_id)) ? Integer.valueOf(user_id) : 0;
    	int flag_cancel  = (NumberUtils.isNumber(flag)) ? Integer.valueOf(flag) : 0;
    	String username = Secure.Security.connected();
        models.User fri = new User();
        fri = userService.getUserByUsername(username);
        //fri.getUser_id();
        fri.setFriend_id(friend_id);
    		if (flag_cancel==0){
    			userService.updateRelationship(fri);
    		}
            updatesService.updateVisited(share_id);
        //PointWall data = new PointWall();
        //data.setUser(user);
        //data.setPoints(points);
        //render(data);
    }
}